home *** CD-ROM | disk | FTP | other *** search
/ Aminet 31 / Aminet 31 (1999)(Schatztruhe)[!][Jun 1999].iso / Aminet / util / libs / chunky_dev.lha / chunky_dev / Demos / Text / TextDemo.c < prev   
Encoding:
C/C++ Source or Header  |  1999-03-17  |  6.0 KB  |  232 lines

  1. //
  2. // chunky.library demo
  3. //
  4. // This example shows how to use the funky text commands...
  5. //
  6. // (c) 1999 Rosande Limited, all rights reserved.
  7. // PUBLIC DOMAIN
  8. //
  9. // http://www.irrelevant.org/~oondy/chunky/
  10.  
  11. #include <exec/types.h>
  12. #include <exec/libraries.h>
  13. #include <clib/chunky_protos.h>
  14. #include <libraries/chunky.h>
  15. #include <pragma/chunky_lib.h>
  16. #include <intuition/intuition.h>
  17. #include <pragma/intuition_lib.h>
  18. #include <pragma/graphics_lib.h>
  19. #include <pragma/exec_lib.h>
  20. #include <pragma/diskfont_lib.h>
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24.  
  25. #include "/_shared/screen.h"
  26. #include "/_shared/waitbutton.h"
  27. #include "/_shared/requester.h"
  28. #include "/_shared/text.h"
  29.  
  30. struct Library *ChunkyBase;
  31.  
  32. void DoDemo(void);
  33. BOOL DoTopazDemo(void);
  34. void DoTextDemo(struct ChunkyPort *cp, int Height);
  35. BOOL DoMultiDemo(void);
  36.  
  37. main()
  38. {
  39.   if(ChunkyBase = OpenLibrary("chunky.library", 4))
  40.   {
  41.     if(DEMO_OpenScreen(640, 480, NULL, NULL))
  42.     {
  43.       CHK_ChooseHardwareMode(GetVPModeID(&DemoScreen->ViewPort));
  44.  
  45.       DoDemo();
  46.     }
  47.   }
  48.   else
  49.     printf("need chunky.library v4\n");
  50.  
  51.   if(DemoScreen) DEMO_CloseScreen();
  52.   if(ChunkyBase) CloseLibrary(ChunkyBase);
  53. }
  54.  
  55. void DoDemo(void)
  56. {
  57.   DEMO_Request("A juicy example!\n\nHow to draw text with chunky.library.\n\n"
  58.                "chunky.library has extensive support for fonts and even has\n"
  59.                "some unique styles which even the graphics.library can't do.\n\n"
  60.                "This example uses your default system font, and also a couple\n"
  61.                "of the standard Workbench fonts.  Onwards...", "Cool", NULL);
  62.  
  63.   if(!DoTopazDemo()) return;
  64.   if(!DoMultiDemo()) return;
  65. }
  66.  
  67. int randmax(int n) { int i; if(n < 1) return(0); for(;;) { i = rand(); if(!(i>n)) return(i); } }
  68.  
  69. BOOL DoTopazDemo(void)
  70. {
  71.   struct TextFont *Topaz80 = NULL;
  72.   struct ChunkyPort *cp = NULL;
  73.   struct TextAttr topaz80attr = {"topaz.font", 8, FS_NORMAL, FPB_ROMFONT };
  74.   int c, x, y, i, len;
  75.   char s[80];
  76.   BOOL ok = FALSE;
  77.  
  78.   // allocate buffer
  79.   if(cp = CHK_InitChunky(320, 200))
  80.   {
  81.     // open topaz font
  82.     if(Topaz80 = OpenFont(&topaz80attr))
  83.     {
  84.       // right then
  85.       // install the font on the ChunkyPort
  86.       // you MUST do this before you can  use any of the text routines
  87.       CHK_SetFont(cp, Topaz80);
  88.  
  89.       DEMO_Request("Okay, topaz.font/8 is open (duh :).  Now draw the text\n"
  90.                    "in JAM2, random colour, and anywhere in ChunkyPort...", NULL, NULL);
  91.  
  92.       sprintf(s, "topaz.font/8!");
  93.  
  94.       // Use CHK_TextLength() to figure out the width of the text
  95.       len = CHK_TextLength(cp, s, strlen(s));
  96.  
  97.       for(i = 0; i < 50; i++)
  98.       {
  99.         c = randmax(255)+1;
  100.         x = randmax(320-len-1);
  101.         // *** NOTE THE VALUE FOR THE Y-COORD!  Add the height of the
  102.         // font to your y-coord!  Very important!
  103.         y = 8+randmax(200-(8*2)-1);
  104.  
  105.         CHK_SetAPen(cp, c);
  106.         CHK_Move(cp, x, y);
  107.         CHK_Text(cp, s);
  108.       }
  109.  
  110.       CHK_DrawChunky(cp, DemoWindow->RPort, 160, 140);
  111.  
  112.       DEMO_Request("Nice!\n\nLet's demonstrate all the text styles...", "Yeh, baby!", NULL);
  113.       DoTextDemo(cp, 8);
  114.       DEMO_Request("..and you can combine other styles too!", "Amazing", NULL);
  115.  
  116.       ok = TRUE;
  117.     }
  118.     else
  119.      printf("can't open topaz.font 8 (are you nuts?)\n");
  120.   }
  121.   else
  122.    printf("no mem in DoTopazDemo()\n");
  123.  
  124.   if(cp) CHK_FreeChunky(cp);
  125.   if(Topaz80) CloseFont(Topaz80);
  126.   return(ok);
  127. }
  128.  
  129. void DoTextDemo(struct ChunkyPort *cp, int Height)
  130. {
  131.   char s[40];
  132.   int y = 8;
  133.  
  134.   CHK_ClearChunky(cp);
  135.  
  136.   // Set some pens with some of the styles use
  137.   CHK_SetABOPen(cp, 1, 3, 2);
  138.   CHK_SetDrMd(cp, JAM2);
  139.  
  140.   // Normal
  141.   CHK_SetAPen(cp, randmax(255)+1);
  142.   CHK_Move(cp, 8, y);
  143.   CHK_SetSoftStyle(cp, FS_NORMAL);  // just for completeness :)
  144.   sprintf(s, "FS_NORMAL text"); CHK_Text(cp, s);
  145.   y += Height + 8;
  146.   // Bold
  147.   CHK_SetAPen(cp, randmax(255)+1);
  148.   CHK_Move(cp, 8, y);
  149.   CHK_SetSoftStyle(cp, FSF_BOLD);
  150.   sprintf(s, "FSF_BOLD text"); CHK_Text(cp, s);
  151.   y += Height + 8;
  152.   // 3D
  153.   CHK_SetDrMd(cp, JAM1);
  154.   CHK_SetAPen(cp, randmax(255)+1);
  155.   CHK_Move(cp, 8, y);
  156.   CHK_SetSoftStyle(cp, FSF_3D);  // chunky.lib specific
  157.   sprintf(s, "FSF_3D text"); CHK_Text(cp, s);
  158.   y += Height + 8;
  159.   // Wide3D
  160.   CHK_SetAPen(cp, randmax(255)+1);
  161.   CHK_Move(cp, 8, y);
  162.   CHK_SetSoftStyle(cp, FSF_WIDE3D);
  163.   sprintf(s, "FSF_WIDE3D text"); CHK_Text(cp, s);
  164.   y += Height + 8;
  165.   // Outline
  166.   CHK_SetAPen(cp, randmax(255)+1);
  167.   CHK_Move(cp, 8, y);
  168.   CHK_SetSoftStyle(cp, FSF_OUTLINE);
  169.   sprintf(s, "FSF_OUTLINE text"); CHK_Text(cp, s);
  170.   y += Height + 8;
  171.   // Embossed
  172.   CHK_SetAPen(cp, randmax(255)+1);
  173.   CHK_Move(cp, 8, y);
  174.   CHK_SetSoftStyle(cp, FSF_EMBOSSED);
  175.   sprintf(s, "FSF_EMBOSSED text"); CHK_Text(cp, s);
  176.   y += Height + 8;
  177.   // Outline and Bold
  178.   CHK_SetDrMd(cp, JAM2);
  179.   CHK_SetAPen(cp, randmax(255)+1);
  180.   CHK_Move(cp, 8, y);
  181.   CHK_SetSoftStyle(cp, FSF_BOLD | FSF_OUTLINE);
  182.   sprintf(s, "FSF_BOLD and FSF_OUTLINE text"); CHK_Text(cp, s);
  183.   CHK_DrawChunky(cp, DemoWindow->RPort, 160, 140);
  184. }
  185.  
  186. struct TextFont *DEMO_OpenDiskFont(STRPTR Name, int YSize)
  187. {
  188.   struct TextAttr ta;
  189.  
  190.   ta.ta_Name = Name;
  191.   ta.ta_YSize = YSize;
  192.   ta.ta_Style = FS_NORMAL;
  193.   ta.ta_Flags = FPF_DISKFONT;
  194.  
  195.   return((struct TextFont *)OpenDiskFont(&ta));
  196. }
  197.  
  198. BOOL DoMultiDemo(void)
  199. {
  200.   // Use multiple fonts
  201.   struct ChunkyPort *cp = NULL;
  202.   struct TextFont *tf =  NULL;
  203.  
  204.   if(cp = CHK_InitChunky(480, 360))
  205.   {
  206.     DEMO_Request("Okay.. let's try some different fonts.\n\nGimme a courier.font...", NULL, NULL);
  207.  
  208.     if(!(tf = DEMO_OpenDiskFont("courier.font", 13))) return(FALSE);
  209.     CHK_SetFont(cp, tf);
  210.     DoTextDemo(cp, 13);
  211.     DEMO_Request("Nice.", "Yes", NULL);
  212.     CloseFont(tf);
  213.  
  214.     if(!(tf = DEMO_OpenDiskFont("diamond.font", 20))) return(FALSE);
  215.     CHK_SetFont(cp, tf);
  216.     DoTextDemo(cp, 20);
  217.     DEMO_Request("Groovy.", "Aye", NULL);
  218.     CloseFont(tf);
  219.  
  220.     if(!(tf = DEMO_OpenDiskFont("opal.font", 12))) return(FALSE);
  221.     CHK_SetFont(cp, tf);
  222.     DoTextDemo(cp, 12);
  223.     DEMO_Request("Right, that's enough.", NULL, NULL);
  224.     CloseFont(tf);
  225.  
  226.     CHK_FreeChunky(cp);
  227.     return(TRUE);
  228.   }
  229.   printf("no mem\n");
  230.   return(FALSE);
  231. }
  232.